package com.ybbase.framework.base.service.impl;

import com.ybbase.framework.base.model.TreeModel;
import com.ybbase.framework.base.service.TreeService;
import com.ybbase.framework.model.po.system.Permission;
import com.ybbase.framework.model.vo.*;
import com.ybbase.framework.model.vo.organ.OrganTreeVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 树结构类型
 * @author cg
 * @date 2019-11-30 22:03
 * @since cloud2.0
 */
@Service
public class TreeServiceImpl implements TreeService {


    /**
     * 递归循环出树列表
     *
     * @param oldList    上级目录列表 TreeModel 树结构实体
     * @param sourceList 全部列表
     * @author cg
     * @date 2019-08-07 16:34
     */
    @Override
    public void sortTree(List<TreeModel> oldList, List<TreeModel> sourceList) {
        for (TreeModel tp:oldList){
            List<TreeModel> childList = new ArrayList<>();
            for (TreeModel sp:sourceList){
                if (sp.getPid().equals(tp.getId())){
                    childList.add(sp);
                }
            }
            if (childList.size()>0){
                tp.setChildren(childList);
                sortTree(tp.getChildren(),sourceList);
            }else{
                tp.setChildren(new ArrayList<>());
            }
        }
    }
    /**
     * 递归出来的的组织机构树形列表
     * @param
     * @return
     * @author cg
     * @date 2020-01-05 18:34
     * @since cloud2.0
     */
    @Override
    public void sortOrganTree(List<OrganTreeVO> firstList, List<OrganTreeVO> sourceList) {
        for (OrganTreeVO tp:firstList){
            List<OrganTreeVO> childList = new ArrayList<>();
            for (OrganTreeVO sp:sourceList){
                if (sp.getParentId().equals(tp.getId())){
                    childList.add(sp);
                }
            }
            if (childList.size()>0){
                tp.setChildren(childList);
                sortOrganTree(tp.getChildren(),sourceList);
            }else{
                tp.setChildren(new ArrayList<>());
            }
        }
    }

    /**
     * 递归循环出权限列表
     *
     * @param oldList    上级目录列表
     * @param sourceList 全部列表
     * @return
     * @author cg
     * @date 2019-12-01 08:54
     * @since cloud2.0
     */
    @Override
    public void sortPermissionTree(List<MenuVO> oldList, List<MenuVO> sourceList) {
        for (MenuVO tp:oldList){
            List<MenuVO> childList = new ArrayList<>();
            for (MenuVO sp:sourceList){
                if (sp.getParentId().equals(tp.getId())){
                    childList.add(sp);
                }
            }
            if (childList.size()>0){
                tp.setList(childList);
                tp.setChildren(childList);
                sortPermissionTree(tp.getList(),sourceList);
                sortPermissionTree(tp.getChildren(),sourceList);
            }else{
                tp.setChildren(new ArrayList<>());
                tp.setList(new ArrayList<>());
            }
        }
    }

    /**
     * 封装前台菜单
     * @author cg
     * @date 2019-08-07 17:28
     * @param tp 权限对象
     * @param arrRoles 选中bottom的按钮字符串
     * @return PermissionTreeVO
     * @since cloud2.0
     */
    @Override
    public MenuVO assembleMenuVO(Permission tp, String arrRoles){
        MenuVO menuVO = new MenuVO();
        menuVO.setId(tp.getId());
        menuVO.setParentId(tp.getParentId());
        menuVO.setArrRoles(arrRoles);
        menuVO.setSpread(false);
        menuVO.setJump(tp.getRouterRedirect());
        menuVO.setIcon(tp.getIcon());
        menuVO.setName(tp.getRouterComponent());
        menuVO.setTitle(tp.getRouterName());
        return menuVO;
    }

    /**
     * 变更数据Boolean
     * @author cg
     * @date 2019-08-07 18:19
     * @param s 判断boolean 0 false 1 true
     * @return Boolean
     * @since cloud2.0
     */
    @Override
    public Boolean getTreeBoolean(Integer s){
        if(s!=null){
            return s!=0;
        }else {
            return false;
        }
    }

    /**
     * 查询权限树的数据
     *
     * @param tp
     * @param check
     * @return
     * @author cg
     * @date 2020-01-12 15:15
     * @since cloud2.0
     */
    @Override
    public PermissionTreeVO assemblePermissionTreeVO(Permission tp, boolean check) {
        PermissionTreeVO permissionTreeVO = new PermissionTreeVO();
        permissionTreeVO.setParentId(tp.getParentId());
        permissionTreeVO.setId(tp.getId());
        permissionTreeVO.setDataScope(tp.getDataScope());
        permissionTreeVO.setTitle(tp.getRouterName());
        CheckArrVO checkArrVO = new CheckArrVO();
        checkArrVO.setType(tp.getDataScope());
        checkArrVO.setChecked(check?"1":"0");
        permissionTreeVO.setCheckArr(checkArrVO);
        return permissionTreeVO;
    }

    /**
     * 排序权限树
     *
     * @param firstList
     * @param sourceList
     * @return
     * @author cg
     * @date 2020-01-12 15:16
     * @since cloud2.0
     */
    @Override
    public void sortPermissionTreeVO(List<PermissionTreeVO> firstList, List<PermissionTreeVO> sourceList) {
        for (PermissionTreeVO tp:firstList){
            List<PermissionTreeVO> childList = new ArrayList<>();
            for (PermissionTreeVO sp:sourceList){
                if (sp.getParentId().equals(tp.getId())){
                    childList.add(sp);
                }
            }
            if (childList.size()>0){
                tp.setChildren(childList);
                sortPermissionTreeVO(tp.getChildren(),sourceList);
            }else{
                tp.setChildren(new ArrayList<>());
            }
        }
    }
}
